package com.qiang.test07;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Demo01 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
        //创建类
        Class sc = Class.forName("com.qiang.test07.Student");

        //获取所有的公共成员方法
        //包括所有的公共方法和继承的方法
        //Method01(sc);

        //获取所有成员方法，不包含继承的
        //Method02(sc);

        //返回单个公共的成员方法
        //Method03(sc);

        //返回单个成员方法
        //Method04(sc);

        //无参有返回值，使用成员方法
        //Method05(sc);

        //有参有返回值使用成员方法
        //Method06(sc);


        //有参无返回值

        Method f2 = sc.getMethod("function2",String.class);
        Student student = (Student) sc.newInstance();
        f2.invoke(student, "张三");
        //System.out.println(zs);
    }

    public static void Method06(Class sc) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Method f4 = sc.getMethod("function4", String.class);
        var student = (Student)sc.newInstance();
        Object lisi = f4.invoke(student, "李四");
        System.out.println(lisi);
    }

    public static void Method05(Class sc) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Method f3 = sc.getMethod("function3");
        Student student =(Student)sc.newInstance();
        Object invoke = f3.invoke(student, null);
        System.out.println(invoke);
    }

    public static void Method04(Class sc) throws NoSuchMethodException {
        Method f2 = sc.getMethod("function2", String.class);
        System.out.println(f2);
        Method show = sc.getDeclaredMethod("show");
        System.out.println(show);
    }

    public static void Method03(Class sc) throws NoSuchMethodException {
        Method mh = sc.getMethod("function1");
        System.out.println(mh);
    }

    public static void Method02(Class<?> sc) {
        Method[] dm = sc.getDeclaredMethods();
        for (Method method : dm) {
            System.out.println(method);
        }
    }

    public static void Method01(Class<?> sc) {
        Method[] mh = sc.getMethods();
        for (Method method : mh) {
            System.out.println(method);
        }
    }
}
